package com.aiyige.page.login.presenter;

import android.app.Activity;

import com.aiyige.MyApp;
import com.aiyige.R;
import com.aiyige.page.login.callbacks.ISendVerifyCodeCallBack;
import com.aiyige.page.login.model.IBindingPhoneModel;
import com.aiyige.page.login.model.impl.BindingPhoneImplement;
import com.aiyige.page.login.view.IBindingPhoneView;
import com.aiyige.utils.ToastX;
import com.vondear.rxtools.RxDataTool;

/**
 *
 */
public class BindingPhonePresenter {
    private IBindingPhoneModel mModel;
    private IBindingPhoneView mView;

    /**
     * 把Activity的需要用到的model通过ILoginView接口传递到Presenter来
     * @param mRegisterView
     */
    public BindingPhonePresenter(IBindingPhoneView mRegisterView)
    {
        this.mView = mRegisterView;
        this.mModel = new BindingPhoneImplement();
    }

    public void next(Activity activity)
    {
        if (!checkInput()) {
            return;
        }
        if (RxDataTool.isNullString(mView.getUserName())) {
            ToastX.show(MyApp.getInstance().getString(R.string.phone_can_not_empty));
            return;
        }
        if (mView.getVerifyCode().length() < 6) {
            ToastX.show(MyApp.getInstance().getString(R.string.verify_code_incomplete));
            return;
        }
        mModel.checkVerifyCode(activity,mView.getUserName(), mView.getVerifyCode(),mView.getThirdOauthBackup());
    }

    public void sendVerifyCode() {
        if (checkInput()) {
            mModel.sendVerifyCode(mView.getMyContext(),mView.getUserName(), new ISendVerifyCodeCallBack() {
                @Override
                public void success(String verifyCode) {
                    mView.setVerifyCode(verifyCode);
                }
            });
        }
    }

    private boolean checkInput() {
        boolean pass = false;
        if (!RxDataTool.isNullString(mView.getUserName())) {
            pass = true;
        } else {
            ToastX.show(MyApp.getAppContext().getString(R.string.phone_can_not_empty));
        }
        return pass;
    }
}
